package com.example.gokchinesefoodmapmcdev.service;

import com.example.gokchinesefoodmapmcdev.dao.OrderInfoDAO;
import com.example.gokchinesefoodmapmcdev.entity.CustomerInfo;
import com.example.gokchinesefoodmapmcdev.entity.DTO.*;
import com.example.gokchinesefoodmapmcdev.entity.MerchantInfo;
import com.example.gokchinesefoodmapmcdev.entity.address.LocationAddress;
import com.example.gokchinesefoodmapmcdev.entity.goods.GoodsSku;
import com.example.gokchinesefoodmapmcdev.entity.order.OrderInfo;
import com.example.gokchinesefoodmapmcdev.entity.order.OrderReturnImage;
import com.example.gokchinesefoodmapmcdev.entity.order.OrderSku;
import com.example.gokchinesefoodmapmcdev.util.ResultData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class OrderServiceImpl implements OrderService{


    @Autowired
    OrderInfoDAO orderInfoDAO;


    /*
    * 根据退款订单Id，查询退款订单详情
    * */
    @Override
    public ResultData selectOrderReturnInfoByReturnOrderId(Integer returnOrderId) {

        //根据returnOrderId，查询出退款订单详情
        OrderReturnDetailDTO orderReturnDetailDTO = orderInfoDAO.selectOrderReturnInfoByReturnOrderId(returnOrderId);

        //根据商户Id查询商户信息,并将查询到的信息放入orderReturnDetailDTO中
        //获取商户ID
        Long merchantId = orderReturnDetailDTO.getMerchantId();
        MerchantInfo merchantInfo = orderInfoDAO.selectMerchantInfoByMerchantId(merchantId);
        orderReturnDetailDTO.setMerchantInfoEntity(merchantInfo);

        //获取customerId,根据customerId查询出用户信息（customerInfoEntity），然后把用户信息放入orderReturnDetailDTO中
        Long customerId = orderReturnDetailDTO.getCustomerId();
        CustomerInfo customerInfo = orderInfoDAO.selectCustomerInfoByCustomerId(customerId);
        orderReturnDetailDTO.setCustomerInfoEntity(customerInfo);

        //根据return_order_id查询出订单退款图片,然后把查询到的信息放入orderReturnListDTOS中
        List<OrderReturnImage> orderReturnImages = orderInfoDAO.selectOrderReturnImageByReturnOrderId(returnOrderId);
        orderReturnDetailDTO.setListOrderReturnImageEntity(orderReturnImages);

        //根据return_order_id,查询出goodsSkuEntity，并把结果放入orderReturnDetailDTO中
        GoodsSku goodsSku = orderInfoDAO.selectGoodsSkuInfoByReturnOrderId(returnOrderId);
        orderReturnDetailDTO.setGoodsSkuEntity(goodsSku);

        //根据return_order_id,查询出orderSkuEntity，并把结果放入orderReturnDetailDTO中
        OrderSku orderSku = orderInfoDAO.selectOrderSkuInfoByReturnOrderId(returnOrderId);
        orderReturnDetailDTO.setOrderSkuEntity(orderSku);

        //返回结果给前端
        ResultData resultData = new ResultData();
        resultData.put("code",0);
        resultData.put("rows",orderReturnDetailDTO);

        return resultData;
    }

    /*
    * 先根据子订单号查询出return_order_id，
    *   然后根据return_order_id和returnOrderImageUrl在order_return_image表中插入图片信息
    * */
    @Override
    public ResultData insertOrderReturnImage(String orderSubNo, String returnOrderImageUrl) {

        //根据子订单号查询出return_order_id
        Integer returnOrderId = orderInfoDAO.selectReturnOrderIdByOrderSubNo(orderSubNo);
        int i = orderInfoDAO.insertOrderReturnImage(returnOrderId, returnOrderImageUrl);
        if(i>0){
            return new ResultData(0,"操作成功！");
        }
        return new ResultData(500,"操作失败");
    }

    /*
    * 发起退款申请
    * */
    @Override
    public ResultData insertOrderReturnInfo(String returnReason, String orderSubNo) {

        //根据子订单号查询出订单信息，
        OrderInfo orderInfo = orderInfoDAO.selectOrderNoByOrderSubNo(orderSubNo);
        //获取订单信息，方便后续的插入一条退款订单信息
        //生成不重复的退款订单号
        String returnNo= "re"+UUID.randomUUID().toString();
        String orderNo = orderInfo.getOrderNo();
        Integer returnCustomer = orderInfo.getCustomerId();
        String orderSn = orderInfo.getOrderSn();
        BigDecimal orderAmount = orderInfo.getOrderAmountTotal();
        Integer customerId = orderInfo.getCustomerId();
        Date gmtCreateTime = new Date();
        //根据订单号查询，看退款订单中是否有该子订单存在，如果没有进行下一步，如果有，则直接返回，
        OrderInfo orderInfo1 = orderInfoDAO.selectOrderReturnInfoByOrderSubNo(orderSubNo);
        if(orderInfo1!=null){
            return new ResultData(200,"该订单已经存在退款，请不要重复提交");
        }
        //插入一条退款订单信息
        int i = orderInfoDAO.insertOrderReturnInfo(returnNo, orderNo, orderSubNo, returnCustomer, orderSn, returnReason, orderAmount, customerId, gmtCreateTime);
        if(i>0){
            return new ResultData(0,"操作成功！");
        }
        return new ResultData(500,"操作失败！");


    }

    /*
    *
    * 退款订单分页列表查询*/
    @Override
    public ResultData orderReturnList(Integer pageSize, Integer pageNumber) {

        //开启分页，该方法调用需要在调用数据库之前
        PageHelper.startPage(pageNumber,pageSize);

        List<OrderReturnListDTO> orderReturnListDTOS = orderInfoDAO.orderReturnList();
        for (int i = 0; i < orderReturnListDTOS.size(); i++) {

            //获取merchantId，根据merchantId查询商户信息（merchantInfoEntity），然后把商户信息放入orderReturnListDTOS中
            Long merchantId = orderReturnListDTOS.get(i).getMerchantId();
            MerchantInfo merchantInfo = orderInfoDAO.selectMerchantInfoByMerchantId(merchantId);
            orderReturnListDTOS.get(i).setMerchantInfoEntity(merchantInfo);

            //获取customerId,根据customerId查询出用户信息（customerInfoEntity），然后把用户信息放入orderReturnListDTOS中
            Long customerId = orderReturnListDTOS.get(i).getCustomerId();
            CustomerInfo customerInfo = orderInfoDAO.selectCustomerInfoByCustomerId(customerId);
            orderReturnListDTOS.get(i).setCustomerInfoEntity(customerInfo);

            //获取order_return_id，根据order_return_id查询图片url（listOrderReturnImageEntity），把结果放入orderReturnListDTOS中
            Integer returnOrderId = orderReturnListDTOS.get(i).getReturnOrderId();
            List<OrderReturnImage> orderReturnImages = orderInfoDAO.selectUrlListByReturnOrderId(returnOrderId);
            orderReturnListDTOS.get(i).setListOrderReturnImageEntity(orderReturnImages);

            //获取order_return_id,根据order_no,order_sub_no，2个参数在order_return、order_sku两张表中查出订单规格信息（orderSkuEntity）
            OrderSkuDTO orderSkuDTO = orderInfoDAO.selectOrderSku(returnOrderId);
            orderReturnListDTOS.get(i).setOrderSkuEntity(orderSkuDTO);

            //获取good_sku_id,根据good_sku_id查询goodsSkuEntity
            Integer goodSkuId = orderSkuDTO.getGoodSkuId();
            GoodsSku goodsSku = orderInfoDAO.selectGoodSkuInfoByGoodSkuId(goodSkuId);
            orderSkuDTO.setGoodsSkuEntity(goodsSku);

        }


        PageInfo<OrderReturnListDTO> pageInfo=new PageInfo<>(orderReturnListDTOS);

        ResultData resultData=new ResultData();

        //构建翻页对象

        resultData.put("pageNo",pageInfo.getPageNum());
        resultData.put("pageSize",pageInfo.getPageSize());
        resultData.put("total",pageInfo.getTotal());
        resultData.put("totalPages",pageInfo.getPages());
        resultData.put("first",1);

        //将翻页对象存入结果集

        resultData.put("rows",orderReturnListDTOS);

        return resultData;


    }

    /*
    * 根据前端传入的订单号，删除该订单
    * */
    @Override
    public ResultData deleteOrderByOrderNo(String orderNo) {
        int i = orderInfoDAO.deleteOrderByOrderNo(orderNo);
        if(i>0){
            return new ResultData(0,"操作成功！");
        }
        return new ResultData(500,"操作失败");
    }

    /*
    * 根据前端传入的订单号，修改订单状态为已经收货（已经收获：3）
    * */
    @Override
    public ResultData updateOrderStatusByOrderNo(String orderNo) {
        int i = orderInfoDAO.updateOrderStatusByOrderNo(orderNo);
        if(i>0){
            return new ResultData(0,"操作成功！");
        }
        return new ResultData(500,"操作失败");
    }

    /*
    * 通过前端传入的订单号，查询订单信息
    * */
    @Override
    public ResultData selectOrderInfoByOrderNo(String orderNo) {

        OrderDetailsDTO orderDetailsDTO = orderInfoDAO.selectOrderInfoByOrderNo(orderNo);
        ResultData resultData=new ResultData();

        List<GoodsInfoDTO> goodsInfoDTOS = orderInfoDAO.selectGoodsInfoByOrderNo(orderNo);

        orderDetailsDTO.setGoods(goodsInfoDTOS);
        resultData.put("code",0);
        resultData.put("rows",orderDetailsDTO);

        return resultData;
    }

    /*
    * 取消订单
    * */
    @Override
    public ResultData cancelOrder(String orderNo) {
        int i = orderInfoDAO.cancelOrder(orderNo);
        if(i>0){
            return new ResultData(0,"操作成功");
        }
        return new ResultData(ResultData.OPERATION_FAIL,"操作失败");
    }


    /*
    * 查询订单列表
    * */
    @Override
    public ResultData selectOrderList(Integer pageSize, Integer pageNumber,Integer orderStatus) {

        //开启分页，该方法调用需要在调用数据库之前
        PageHelper.startPage(pageNumber,pageSize);

        List<OrderInfoDTO> orderInfoDTOS = orderInfoDAO.selectOrderList(orderStatus);

        PageInfo<OrderInfoDTO> pageInfo=new PageInfo<>(orderInfoDTOS);

        ResultData resultData=new ResultData();

        //构建翻页对象

//        Map<String, Object> rows = new HashMap<>();

        resultData.put("pageNo",pageInfo.getPageNum());
        resultData.put("pageSize",pageInfo.getPageSize());

        //将翻页对象存入结果集

//        rows.put("rows",goodsInfos);

        resultData.put("rows",orderInfoDTOS);

        return resultData;





    }
}
